package com.yl.immodule.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.unionbroad.greendao.Channel;
import com.yl.immodule.bean.YChannel;
import com.yl.immodule.databinding.UnionChannellistItemBinding;
import com.yl.immodule.fragment.model.ChannelListViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 使用MVVM模式的频道列表适配器
 */
public class ChannelListAdapter extends ListAdapter<YChannel, ChannelListAdapter.ChannelViewHolder> {

    private final ChannelClickListener channelClickListener;
    private ChannelListViewModel viewModel;

    public ChannelListAdapter(ChannelClickListener listener, ChannelListViewModel viewModel) {
        super(new ChannelDiffCallback());
        this.channelClickListener = listener;
        this.viewModel = viewModel;
    }

    /**
     * 将指定频道置顶并显示power图标
     * @param yChannel 需要置顶的频道
     */
    public void toTopChannel(YChannel yChannel) {
        // 获取当前列表数据
        List<YChannel> currentList = new ArrayList<>(getCurrentList());
        
        // 如果列表为空或channel为null，直接返回
        if (currentList.isEmpty() || yChannel == null) {
            return;
        }

        // 找到目标channel的位置
        int originalPosition = -1;
        for (int i = 0; i < currentList.size(); i++) {
            if (currentList.get(i).getChannel().getChannelNumber().equals(yChannel.getChannel().getChannelNumber())) {
                originalPosition = i;
                break;
            }
        }

        // 如果找到了目标channel
        if (originalPosition != -1) {
            // 将channel从原位置移除
            YChannel targetChannel = currentList.remove(originalPosition);
            targetChannel.setConnect(yChannel.isConnect());
            // 将channel添加到列表开头
            currentList.add(0, targetChannel);
            
            // 提交新的列表到适配器
            submitList(currentList, () -> {
                // 通知item更新，以显示power图标
                notifyItemChanged(0);
            });
        }
    }

    @NonNull
    @Override
    public ChannelViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        UnionChannellistItemBinding binding = UnionChannellistItemBinding.inflate(
                LayoutInflater.from(parent.getContext()), parent, false);
        return new ChannelViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ChannelViewHolder holder, int position) {
        YChannel yChannel = getItem(position);
        holder.bind(channelClickListener, viewModel, yChannel);
    }

    /**
     * 频道列表项ViewHolder
     */
    static class ChannelViewHolder extends RecyclerView.ViewHolder {
        private final UnionChannellistItemBinding binding;

        public ChannelViewHolder(UnionChannellistItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void bind(ChannelClickListener listener, ChannelListViewModel viewModel, YChannel yChannel) {
            binding.setViewModel(viewModel);
            binding.setChannel(yChannel);
            binding.setClickListener(listener);
            binding.executePendingBindings();
        }
    }

    /**
     * 频道差异比较回调
     */
    static class ChannelDiffCallback extends DiffUtil.ItemCallback<YChannel> {
        @Override
        public boolean areItemsTheSame(@NonNull YChannel oldItem, @NonNull YChannel newItem) {
            // 使用频道编号作为唯一标识
            return oldItem.getChannel().getChannelNumber() == newItem.getChannel().getChannelNumber();
        }

        @Override
        public boolean areContentsTheSame(@NonNull YChannel oldItem, @NonNull YChannel newItem) {
            // 比较频道的所有相关属性
            Channel oldChannel = oldItem.getChannel();
            Channel newChannel = newItem.getChannel();
            return oldChannel.getChannelName().equals(newChannel.getChannelName()) &&
                   oldChannel.getChannelType().equals(newChannel.getChannelType()) &&
                   oldChannel.getCreateType().equals(newChannel.getCreateType()) &&
                   oldItem.isListener() == newItem.isListener() &&
                   oldItem.isConnect() == newItem.isConnect();
        }
    }
} 